/*
 * Copyright (c) 2010, Thiago Campos Viana.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Thiago Campos Viana nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kernel.gamecode;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import lib.MapParser;

/**
 *
 * @author Thiago Campos Viana
 */
public class Region {

    /**
     *
     */
    public List<Player> players = new ArrayList<Player>();
    /**
     *
     */
    public List<Monster> monsters = new ArrayList<Monster>();
    /**
     *
     */
    public List<NPC> npcs = new ArrayList<NPC>();
    /**
     *
     */
    public List<Item> items = new ArrayList<Item>();
    /**
     *
     */
    public List<Trap> traps = new ArrayList<Trap>();
    /**
     *
     */
    public List<Door> doors = new ArrayList<Door>();
    /**
     *
     */
    public ByteBuffer bytes;
    /**
     *
     */
    public boolean active;

    /* Tile Types:
     * 0 - walkable
     * 1 - blocked
     * 2 - blocked by gameobject
     * 3 - UpStairs up
     * 4 - UpStairs right
     * 5 - UpStairs down
     * 6 - UpStairs left
     * 7 - DownStairs up
     * 8 - DownStairs right
     * 9 - DownStairs down
     * 10 - DownStairs left
     * 11 - Blocked to NPCS and Monsters
     * 12 - Closed Door
     * 13 - Openned Door
     * 14 - Trap
     */
    /**
     *
     */
    public byte[][][] tileMap = new byte[16][100][100];
    private byte id;

    /**
     *
     */
    public Region(byte id) {
        this.id = id;
        tileMap[0]=MapParser.getMap((byte)0, (byte)0, (byte)0);

        doors.add(new Door(100, (byte) 0, (byte) 3, (byte) 3));

    }

    /**
     *
     */
    public void update() {
        if (players.isEmpty()) {
            return;
        }

        int typeBuffer = 6;

        int bufferSize = Player.bufferSize * players.size()
                + Monster.bufferSize * monsters.size()
                + NPC.bufferSize * npcs.size()
                //+Trap.bufferSize*traps.size()
                //+Door.bufferSize*doors.size()
                //+Item.bufferSize*items.size()
                + typeBuffer;

        bytes = ByteBuffer.allocate(bufferSize);

        bytes.putShort((short) players.size());
        Iterator<Player> player = players.iterator();
        while (player.hasNext()) {
            bytes.put(player.next().update());
        }

        bytes.putShort((short) monsters.size());
        Iterator<Monster> monster = monsters.iterator();
        while (monster.hasNext()) {
            bytes.put(monster.next().update());
        }

        bytes.putShort((short) npcs.size());
        Iterator<NPC> npc = npcs.iterator();
        while (npc.hasNext()) {
            bytes.put(npc.next().update());
        }

//        bytes.putShort((short)traps.size());
//        Iterator<Trap> trap = traps.iterator();
//        while(trap.hasNext()){
//            bytes.put(trap.next().update());
//        }
//
//        bytes.putShort((short)items.size());
//        Iterator<Item> item = items.iterator();
//        while(item.hasNext()){
//            bytes.put(item.next().update());
//        }
//
//        bytes.putShort((short)doors.size());
//        Iterator<Door> door = doors.iterator();
//        while(door.hasNext()){
//            bytes.put(door.next().update());
//        }

    }

    /**
     *
     * @param name
     * @param address
     * @return
     */
    public void addPlayer(Player p) {
        players.add(p);
    }

    /**
     *
     * @return
     */
    public byte[] getBytes() {
        return bytes.array();
    }
}
